package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.design.entity.Process;
import com.be.beadmin.design.mapper.ProcessMapper;
import com.be.beadmin.design.service.ProcessService;
import com.be.beadmin.product.dto.input.CapacityByDay;
import com.be.beadmin.product.dto.input.PlantCapacityBaseDto;
import com.be.beadmin.product.dto.output.BaseCapacity;
import com.be.beadmin.product.dto.output.DateRangeCapacity;
import com.be.beadmin.product.dto.output.PlantCapacityBaseEx;
import com.be.beadmin.product.entity.PlantCapacityBase;
import com.be.beadmin.product.service.PlantCapacityBaseService;
import com.be.beadmin.product.service.PlantCapacityCalendarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author ljh
* @date 2023-03-26
**/
@Api(tags = {"生产制造-产能基础数据管理"})
@RestController
public class PlantCapacityBaseController {

    @Autowired
    private  PlantCapacityBaseService plantCapacityBaseService;

    @Autowired
    private PlantCapacityCalendarService plantCapacityCalendarService;

    @Autowired
    private ProcessService  processService;

    @Autowired
    private ProcessMapper processMapper;

    @ApiOperation("判断某工序是否存在产能数据")
    @PostMapping(value = "/product/judgePlantCapacityBaseByProcessCode")
    public ResultModel judgePlantCapacityBaseByProcessCode(@ApiParam(value = "工序Code") @RequestParam String processCode)  {
        PlantCapacityBase plantCapacityBase = plantCapacityBaseService.getProcessByProcessCodeAndDelete(processCode,0);
        return ResultModel.success(plantCapacityBase) ;
    }

    @ApiOperation("获取各工序的产能基础")
    @PostMapping(value = "/product/getPlantCapacityBase")
    public ResultModel getPlantCapacityBase(@ApiParam(value = "工序id(指定工序,若为null,则表示全部工序之和)") @RequestParam String processId)  {
        Process process = processService.getProcessById(processId);
        BaseCapacity baseCapacity = plantCapacityBaseService.getPlantCapacityBase(processMapper.toDto(process));
        return ResultModel.success(baseCapacity) ;
    }

    @ApiOperation("获取指定天数内的各工序产能")
    @PostMapping(value = "/product/getAllPlantCapacityCalendar")
    public ResultModel getPlantCapacityCalendar(@RequestBody CapacityByDay capacityByDay)  {
        Process process = processService.getProcessById(capacityByDay.getProcessId());
        List<DateRangeCapacity> dateRangeCapacityList = plantCapacityCalendarService.computeRealCapacitySumByDayAndRange(capacityByDay.getDate(), capacityByDay.getRange(),process);
        return ResultModel.success(dateRangeCapacityList) ;
    }

    @ApiOperation("产能基础数据分页信息")
    @PostMapping(value = "/product/getPlantCapacityBaseByPage")
    public ResultModel getPlantCapacityBaseByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(plantCapacityBaseService.getPlantCapacityBaseByPage(pageEntity)) ;
    }

    @ApiOperation("新增产能数据")
    @PostMapping("/product/addPlantCapacityBase")
    public ResultModel addPlantCapacityBase(@RequestBody PlantCapacityBaseDto plantCapacityBaseDto){
        plantCapacityBaseService.addPlantCapacityBase(plantCapacityBaseDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("删除产能数据")
    @PostMapping("/product/delPlantCapacityBase")
    public ResultModel delPlantCapacityBase(@RequestBody PlantCapacityBaseDto plantCapacityBaseDto){
        plantCapacityBaseService.delPlantCapacityBase(plantCapacityBaseDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("编辑产能数据")
    @PostMapping("/product/updatePlantCapacityBase")
    public ResultModel updatePlantCapacityBase(@RequestBody PlantCapacityBaseDto plantCapacityBaseDto){
        plantCapacityBaseService.updatePlantCapacityBase(plantCapacityBaseDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("导出产能数据")
    @PostMapping("/product/exportPlantCapacityBase")
    public void exportPlantCapacityBase(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PlantCapacityBaseEx> plantCapacityBasePage = plantCapacityBaseService.getPlantCapacityBaseByPage(pageEntity);
        List<PlantCapacityBaseEx> plantCapacityBaseExes = plantCapacityBasePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PlantCapacityBase接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PlantCapacityBaseEx.class).sheet("sheet1").doWrite(plantCapacityBaseExes);
    }
}